﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ipaidabribe.Code;


namespace ipaidabribe.AdminCP
{
    public partial class index : System.Web.UI.Page
    {
        public int page = 1;
        public int totalpage;
        public bool sa = false;
        public string html_pager = "";
        public string _action = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LOGIN"] != null)
            {
                if (Session["LOGIN"].ToString() != "1")
                    Response.Redirect("Login.aspx");
            }
            else
            {
                    Response.Redirect("Login.aspx");
            }

            if (Request.QueryString["page"] != null)
            {
                int.TryParse(Request.QueryString["page"].ToString(), out page);
            }
            if (Request.QueryString["action"] != null)
            {
                _action = Request.QueryString["action"].ToString();
            }

            var db = new ipaidabribeEntities();
            var lastest = new List<Bribe>();
            if (_action == "")
                lastest = db.Bribes.OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();
            else if (_action == "show")
                lastest = db.Bribes.Where(t=>t.Enable == true).OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();
            else if (_action == "hidden")
                lastest = db.Bribes.Where(t => t.Enable == false).OrderByDescending(t => t.Id).Skip((page - 1) * 15).Take(15).ToList();

            foreach (var bribe in lastest)
            {
                if (HttpUtility.HtmlDecode(bribe.Content).Length > 400)
                {
                    bribe.Content = HttpUtility.HtmlDecode(bribe.Content);
                    var contentArr = bribe.Content.Split(' ');
                    bribe.Content = bribe.Content.Substring(0, bribe.Content.IndexOf(' ', 400)) + " ...";
                    bribe.Content += " <a href='edit.aspx?ID=" + bribe.Id + "'><b>Edit</b></a>";
                }
                else
                {
                    bribe.Content = HttpUtility.HtmlDecode(bribe.Content);

                }

                bribe.Content = bribe.Content.Replace("</div>", String.Empty);
                bribe.Content = bribe.Content.Replace("<div>", "<br>");

                double d = 0;
                bool abc = double.TryParse(bribe.AmountPaid, out d);
                if (abc)
                    bribe.AmountPaid = String.Format("{0:0,0}", d) + " VND";
                if (bribe.AmountPaid == "00 VND")
                    bribe.AmountPaid = "0 VND";
            }
            Repeater1.DataSource = lastest;
            Repeater1.DataBind();

            //phan trang html
            var countRow = 0;
            if (sa == false)
            {
                countRow = db.Bribes.Where(t => t.Enable != false).Count();
            }
            else
            {
                countRow = db.Bribes.Count();
            }


            totalpage = countRow % 15 == 0 ? countRow / 15 : (countRow / 15) + 1;
            var current_page = page;
            var total_page = totalpage;
            //show pager
            var start = current_page - 5;
            if (start < 0)
            {
                start = 1;
            }
            var end = current_page + 5;
            if (current_page < 5)
            {
                end = current_page + 9;
            }
            if (end > total_page)
            {
                end = total_page;
            }
            //foeach

            //trang dau
            //trang truoc
            if (current_page > 1)
            {
                html_pager += "<a data-role=\"button\" title=\"\" href=\"index.aspx\">« đầu</a>&nbsp;";
                html_pager += "<a data-role=\"button\" title=\"\" href=\"index.aspx?page=" + (current_page - 1) + "\">‹ trước</a>&nbsp;";
            }

            for (var i = start; i <= end; i++)
            {
                html_pager += "";
                if (i == current_page)
                {
                    html_pager += "<a style=\"color: #FF3300;\" data-role=\"button\" title=\"\" href=\"javascript:void(0);\">" + current_page + "</a>&nbsp;";
                }
                else
                {
                    html_pager += "<a data-role=\"button\" title=\"\" href=\"index.aspx?page=" + i + "\">" + i + "</a>&nbsp;";
                }
            }
            //sau va cuoi cung
            if (current_page < total_page)
            {
                html_pager += "<a data-role=\"button\" title=\"\" href=\"index.aspx?page=" + (current_page + 1) + "\">sau ›</a>&nbsp;";
                html_pager += "<a data-role=\"button\" title=\"\" href=\"index.aspx?page=" + total_page + "\">cuối »</a>&nbsp;";
            }
        }

       
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            var context = new ipaidabribeEntities();

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var imgStatus = (HtmlImage)e.Item.FindControl("ShowHidden");
                if (DataBinder.Eval(e.Item.DataItem, "Enable") != null)
                {
                    if ((bool)DataBinder.Eval(e.Item.DataItem, "Enable") == true)
                    {
                        imgStatus.Src = "img/icon_check.gif";
                        imgStatus.Alt = "Show";
                    }
                    else
                    {
                        imgStatus.Src = "img/icon_nocheck.gif";
                        imgStatus.Alt = "Show";
                    }
                }
            }
        }
    }
}